﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <malloc.h>

void InsertSort(int* arr, int n) //默认按升序实现
{
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = arr[end + 1]; //arr[end+1]是待排序的关键字
		while (end >= 0)
		{
			if (arr[end] > tmp)
			{
				arr[end + 1] = arr[end];
				end--;
			}
			else
			{
				break;
			}
		}
		arr[end + 1] = tmp;
	}
}

void BinaryInsertSort(int* arr, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		int tmp = arr[i + 1];
		int left = 0, right = i;
		while (left <= right)
		{
			int m = (left + right) / 2;
			if (tmp < arr[m])
			{
				right = m - 1;
			}
			else
			{
				left = m + 1; //无论插入位置在数组中间还是末尾，left 始终指向第一个大于 tmp 的元素位置
			}
		}
		// 将插入位置之后的元素后移
		for (int j = i; j >= left; j--)
		{
			arr[j + 1] = arr[j];
		}

		arr[left] = tmp;
	}
}

void ShellSort(int* arr, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = arr[end + gap]; //取同一组内有序表最后一个元素的下一个位置的元素
			while (end >= 0)
			{
				if (arr[end] > tmp)
				{
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			arr[end + gap] = tmp;
		}
	}
}

void BubbleSort(int* arr, int n)
{
	for (int i = 0; i < n - 1; i++) //排序n-1趟
	{
		int flag = 0;
		for (int j = 0; j < n - i - 1; j++) //每趟排序要比较n-i-1次来确定一个数
		{
			if (arr[j] > arr[j + 1])
			{
				flag = 1;
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;
			}
		}
		//经过一趟排序后检验是否有序
		if (flag == 0)
		{
			break;
		}
	}
}

int _QuickSort(int* arr, int left, int right)
{
	int keyi = left; //默认选最左边为基准值
	++left;

	while (left <= right)
	{
		//左边找大的
		while (left <= right && arr[left] < arr[keyi])
		{
			left++;
		}
		//右边找小的
		while (left <= right && arr[right] > arr[keyi])
		{
			right--;
		}
		if (left <= right)
		{
			int tmp = arr[left];
			arr[left] = arr[right];
			arr[right] = tmp;
			left++;
			right--;
		}
	}

	int tmp = arr[keyi];
	arr[keyi] = arr[right];
	arr[right] = tmp;
	return right;//返回基准值的位置
}

void QuickSort(int* a, int left, int right)
{
	if (left >= right) 
	{
		return;
	}
	int mid = _QuickSort(a, left, right);
	QuickSort(a, left, mid - 1);
	QuickSort(a, mid + 1, right);
}

void swap(int* x, int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}

void SelectSort(int* arr, int n)
{
	int begin = 0;
	int end = n - 1;
	while (begin < end)
	{
		int mini = begin, maxi = begin;
		for (int i = begin; i <= end; i++)
		{
			if (arr[i] > arr[maxi])
			{
				maxi = i;
			}
			if (arr[i] < arr[mini])
			{
				mini = i;
			}
		}
		//注意处理特殊情况,因为交换有顺序
		if (begin == maxi)
		{
			maxi = mini;
		}
		swap(&arr[mini], &arr[begin]);
		swap(&arr[maxi], &arr[end]);
		++begin;
		--end;
	}
}

//向下调整算法
void AdjustDown(int* arr, int parent, int n)
{
	int child = parent * 2 + 1;
	while (child < n)
	{
		//这里建立大根堆，找最小的孩子然后跟父亲比
		if (child + 1 < n && arr[child] < arr[child + 1])
		{
			child++;
		}
		if (arr[child] > arr[parent])
		{
			swap(&arr[child], &arr[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;//调整结束
		}
	}
}

void HeapSort(int* arr, int n)
{
	//child:n-1 parent:(n-1-1)/2
	for (int i = (n - 1 - 1) / 2; i >= 0; i--) //从最后一个父节点开始逆序向下调整建堆
	{
		AdjustDown(arr, i, n);
	}
	//此时已经建好大堆
	int end = n - 1;
	while (end > 0)
	{
		swap(&arr[0], &arr[end]);
		AdjustDown(arr, 0, end);
		end--;
	}
}


void MergeSort(int* arr, int left, int right, int* tmp)
{
	if (left >= right)
	{
		return;
	}
	int mid = (left + right) / 2;
	//根据mid划分为两个子序列[left,mid] [mid+1,right]
	MergeSort(arr, left, mid, tmp);	
	MergeSort(arr, mid + 1, right, tmp);

	//合并[left,mid]和[mid+1,right]
	int begin1 = left, end1 = mid;
	int begin2 = mid + 1, end2 = right;

	int index = begin1;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (arr[begin1] < arr[begin2])
		{
			tmp[index++] = arr[begin1++];
		}
		else {
			tmp[index++] = arr[begin2++];
		}
	}

	//循环结束后可能存在某个个序列中的数据没有全部存放到tmp中
	while (begin1 <= end1)
	{
		tmp[index++] = arr[begin1++];
	}
	while (begin2 <= end2)
	{
		tmp[index++] = arr[begin2++];
	}

	//将数据挪回到arr中
	for (int i = left; i <= right; i++)
	{
		arr[i] = tmp[i];
	}
}

void Print(int* arr, size_t len);
int main()
{
	int arr[10] = { 5,9,1,4,3,7,2,8,6,0 };
	size_t len = sizeof(arr) / sizeof(arr[0]);
	Print(arr, len);
	/*InsertSort(arr, len);*/
	/*BinaryInsertSort(arr, len);*/
	//ShellSort(arr, len);
	//BubbleSort(arr, len);
	/*QuickSort(arr, 0, len-1);*/
	/*SelectSort(arr, len);*/
	/*HeapSort(arr, len);*/
	int* tmp = (int*)malloc(sizeof(int) * len);
	MergeSort(arr, 0, len - 1, tmp);
	free(tmp);
	Print(arr, len);
	return 0;
}

void Print(int* arr, size_t len)
{
	for (int i = 0; i < len; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}